<template>
  <div>
    <FormPanel ref="formpanel2" :list="formList2" labelWidth="120" :isSubmitBtn="false" @formChange="formchangeFuns" />
    <FilterTable
      ref="basetables"
      :columns="columns1"
      columnsRef="partPartOutboundInternalReceiveEditPartPage"
      :dataSource="emptyTableData"
      :params="fetchParams"
      :datakey="'rows'"
      :onColumnsChange="columns1 => (this.columns1 = columns1)"
      :height="'200'"
      :onSyncTableData="tablechangeFun"
    ></FilterTable>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9999,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="confirmHandle">确认</el-button>
    </div>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { getStorageList, getaddPartList } from '@/api/part/partInventory/adjustInventory/index';
import { getAppId } from '@/assets/js/auth';
export default {
  mixins: [dictionary],
  props: ['editRowData', 'forReturn'],
  data() {
    this.basetables = null;
    return {
      formList2: this.createFroms(),
      fetchParams: {
        xhrAbort: true,
        sort: '',
        order: 'asc',
        limit: '10',
        offset: '0'
      },
      columns1: this.createTables(),
      storageData: {},
      emptyTableData: [], // 用于清空表格数据
      fromSubmitdata: null
    };
  },
  created() {
    this.getlist();
  },
  mounted() {
    // this.checkAppId();
    this.basetables = this.$refs.basetables.$refs.pageTable;
    if (this.forReturn) {
      this.formList2[3].initialValue = this.editRowData.OUT_QUANTITY * -1;
      this.formList2[5].initialValue = this.editRowData.OUT_AMOUNT * -1;
    } else {
      this.formList2[3].initialValue = this.editRowData.OUT_QUANTITY;
      this.formList2[5].initialValue = this.editRowData.OUT_AMOUNT;
    }
    this.formList2[0].initialValue = this.editRowData.STORAGE_CODE;
    this.formList2[1].initialValue = this.editRowData.PART_NO;
    this.formList2[2].initialValue = this.editRowData.PART_NAME;
    this.formList2[4].initialValue = this.editRowData.OUT_PRICE;

    this.getpartlist(this.editRowData);
  },
  methods: {
    createFroms() {
      return [
        { type: 'SELECT', label: '仓库', fieldName: 'STORAGE_CODE', itemList: [], disabled: true },
        { type: 'INPUT', label: '配件代码', fieldName: 'PART_NO', rules: [{ required: true, message: '请输入配件代码', trigger: 'blur' }], disabled: true },
        { type: 'INPUT', label: '配件名称', fieldName: 'PART_NAME', disabled: true },
        {
          type: 'INPUT_NUMBER',
          label: '出库数量',
          min: 0.01,
          precision: 2,
          fieldName: 'OUT_QUANTITY',
          change: val => {
            this.changemNum(val);
          },
          rules: [{ required: true, message: '请输入出库数量', trigger: 'blur' }],
          disabled: false
        },
        {
          type: 'INPUT_NUMBER',
          label: '出库单价',
          min: 0.0,
          precision: 2,
          hidden: true,
          fieldName: 'SALES_PRICE',
          change: val => {
            this.changemoney(val);
          },
          rules: [{ required: true, message: '请输入出库单价', trigger: 'blur' }],
          disabled: false
        },
        {
          type: 'INPUT_NUMBER',
          label: '出库金额',
          min: 0.0,
          precision: 2,
          hidden: true,
          fieldName: 'SALES_AMOUNT',
          rules: [{ required: true, message: '请输入出库金额', trigger: 'blur' }],
          disabled: true
        }
      ];
    },
    createTables() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        { title: '含税销售价', dataIndex: 'SALES_PRICE', precision: 2, sorter: true, filter: true, filterType: 'number' },
        { title: '终端建议零售VIP价格', dataIndex: 'NOT_TAX_INSTRUCT_PRICE', precision: 2, sorter: true, filter: true, filterType: 'number' },
        { title: '常规订货价', dataIndex: 'PLAN_PRICE', precision: 2, sorter: true, filter: true, filterType: 'number' },
        { title: '成本单价', dataIndex: 'COST_PRICE', precision: 2, sorter: true, filter: true, filterType: 'number' },
        { title: '索赔价', dataIndex: 'CLAIM_PRICE', precision: 2, sorter: true, filter: true, filterType: 'number' },
        { title: '终端建议零售价格', dataIndex: 'INSURANCE_PRICE', precision: 2, sorter: true, filter: true, filterType: 'number' },
        // { title: '最低销售限价', dataIndex: 'MIN_LIMIT_PRICE', precision: 2, sorter: true, filter: true, filterType: 'number' },
        // { title: '销售限价', dataIndex: 'LIMIT_PRICE', precision: 2, sorter: true, filter: true, filterType: 'number' },
        { title: '最大库存', dataIndex: 'MAX_STOCK', precision: 2, sorter: true, filter: true, filterType: 'number' },
        { title: '最小库存', dataIndex: 'MIN_STOCK', precision: 2, sorter: true, filter: true, filterType: 'number' },
        { title: '账面库存', dataIndex: 'STOCK_QUANTITY', precision: 2, sorter: true, filter: true, filterType: 'number' },
        { title: '可用库存', dataIndex: 'USEABLE_STOCK', precision: 2, sorter: true, filter: true, filterType: 'number' },
        // {title: '借进数量',  dataIndex: 'BORROW_QUANTITY',  sorter: true,  filter: true, filterType: 'input'},
        // {title: '借出数量',  dataIndex: 'LEND_QUANTITY',  sorter: true,  filter: true, filterType: 'input'},
        { title: '锁定量', dataIndex: 'LOCKED_QUANTITY', precision: 2, sorter: true, filter: true, filterType: 'number' },
        { title: '替代配件', dataIndex: 'OPTION_NO', sorter: true, filter: true, filterType: 'input' },
        { title: '替代件库存', dataIndex: 'OPTION_STOCK', sorter: true, filter: true, filterType: 'input' },
        {
          title: '配件类别',
          dataIndex: 'PART_GROUP_CODE',
          sorter: true,
          editable: false,
          editType: 'select',
          editItems: this.createDictList(8247),
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList(8247)
        },
        { title: '单位', dataIndex: 'UNIT_NAME', sorter: true, filter: true, filterType: 'input' }
      ];
    },
    // 获取数据字典数据
    async getlist() {
      this.storageData = (await getStorageList({})).data;
      this.formList2[0].itemList = this.storageData.map(x => ({ text: x.STORAGE_NAME, value: x.STORAGE_CODE }));
    },
    formchangeFuns(val) {
      console.log(val);
      this.fromSubmitdata = val; // {...val};
    },
    async getpartlist(val) {
      this.emptyTableData = [];
      let res = (await getaddPartList(val.STORAGE_CODE, val.PART_NO)).data.rows;
      this.basetables.EXECUTE_INSERT(res);
    },
    changemoney(val) {
      console.log(val, 'input');
      let num = this.formList2[3].initialValue;
      this.formList2[5].initialValue = val * num;
    },
    changemNum(val) {
      console.log(val, '改变');
      let money = this.formList2[4].initialValue;
      this.formList2[5].initialValue = val * money;
    },
    // 关闭弹窗
    closeDialog() {
      this.$emit('close', false);
    },
    tablechangeFun(val) {
      console.log(val);
      this.tableslist = val;
    },
    async checkAppId() {
      let appId = await getAppId();
      if (appId == 'beijing') {
        this.formList2.find(x => x.fieldName == 'SALES_PRICE').hidden = true;
        this.formList2.find(x => x.fieldName == 'SALES_AMOUNT').hidden = true;
      }
    },
    // 添加事件提交表单
    confirmHandle() {
      // debugger;
      let json = this.tableslist[0];
      this.fromSubmitdata = null;
      this.$refs.formpanel2.SUBMIT_FORM();
      if (this.fromSubmitdata != null) {
        var flag = 2; // 是否记录缺料信息1为是，2为否
        if (this.forReturn) {
          // 退料
          json.OUT_QUANTITY = this.fromSubmitdata.OUT_QUANTITY * -1;
          json.OUT_PRICE = this.fromSubmitdata.SALES_PRICE;
          json.OUT_AMOUNT = this.fromSubmitdata.SALES_AMOUNT * -1;
          json.COST_AMOUNT = (json.COST_PRICE * this.fromSubmitdata.OUT_QUANTITY * -1).toFixed(2);
        } else {
          // 新增
          json.OUT_QUANTITY = this.fromSubmitdata.OUT_QUANTITY;
          json.OUT_PRICE = this.fromSubmitdata.SALES_PRICE;
          json.OUT_AMOUNT = this.fromSubmitdata.SALES_AMOUNT;
          json.COST_AMOUNT = (json.COST_PRICE * this.fromSubmitdata.OUT_QUANTITY).toFixed(2);
        }
        json.editIndex = this.editRowData.index;

        // if(this.fromSubmitdata.SALES_PRICE<json.COST_PRICE){
        //     this.$notify({ title: '警告信息', message: '配件销售单价小于成本单价！', type: 'warning' });
        //     return;
        // }
        if (json.FLAG && json.FLAG == '1') {
        } else {
          if (!this.forReturn && this.fromSubmitdata.OUT_QUANTITY > json.USEABLE_STOCK) {
            // for (var i = 0; i < this.storageData.length; i++) {
            //   if (this.storageData[i].STORAGE_CODE == this.formList2[0].initialValue) {
            //     if (this.storageData[i].IS_NEGATIVE == 10041002) {
            //       this.$notify({ title: '警告信息', message: '出库数量大于可用数量，该仓库不允许负库存，不可以出库！', type: 'warning' });
            //       return;
            //     }
            //   }
            // }
            this.$confirm('是否记录缺料明细？', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            })
              .then(async () => {
                flag = 1;
                json.FLAG = flag;
                let formdata = Object.assign({}, json);
                this.$emit('editPart', formdata);
              })
              .catch(() => {
                flag = 2;
                json.FLAG = flag;
                let formdata = Object.assign({}, json);
                this.$emit('editPart', formdata);
              });
          } else {
            json.FLAG = flag;
            let formdata = Object.assign({}, json);
            this.$emit('editPart', formdata);
          }
        }
      }
    }
  }
};
</script>
